//
//  handleData.m
//  CTM
//
//  Created by jsj on 16/4/6.
//  Copyright © 2016年 青岛晨之晖信息服务有限公司. All rights reserved.
//

#import "handleData.h"
#import <objc/runtime.h>

@implementation handleData
#pragma mark - 将数据字典 dic 转换成 classname 对应类型的model数据
+(id)modelWithClass:(Class)classname andDataDic:(NSDictionary *)dic
{
    id obj = [[classname alloc] init];
    unsigned int count = 0;
    Ivar * ivar = class_copyIvarList(classname, &count);
    for (int i = 0; i<count; i++) {
        Ivar var = ivar[i];
        const char * varName = ivar_getName(var);
        NSString * name = [NSString stringWithUTF8String:varName];
        name = [name substringWithRange:NSMakeRange(1,name.length-1)];
        const char * varType = ivar_getTypeEncoding(var);
        NSString * type = [NSString stringWithUTF8String:varType];//参数类型
        type = [type stringByReplacingOccurrencesOfString:@"@" withString:@""];//过滤@
        type = [type stringByReplacingOccurrencesOfString:@"\"" withString:@""];//过滤"字符
        type = [type stringByReplacingOccurrencesOfString:@"\\" withString:@""];//过滤\字符
        if ([name isEqualToString:@"_id"]) {
            name = [name substringWithRange:NSMakeRange(1,name.length-1)];
        }
        id value = [self filterNullValue:[dic objectForKey:name] andType:type];
        object_setIvar(obj, var, value);
    }
    return obj;
}

#pragma mark - 将数据数组 array 转换成元素类型为 classname 的数组
+(NSArray *)arrayWithClass:(Class)classname andDataArray:(NSArray *)array
{
    NSMutableArray * dataArray = [[NSMutableArray alloc] init];
    for (int i = 0; i<array.count; i++) {
        id obj = [[classname alloc] init];
        unsigned int count = 0;
        Ivar * ivar = class_copyIvarList(classname, &count);
        for (int j = 0; j<count; j++) {
            Ivar var = ivar[j];
            const char * varName = ivar_getName(var);
            NSString * name = [NSString stringWithUTF8String:varName];//参数名
            name = [name substringWithRange:NSMakeRange(1,name.length-1)];//去掉前面的"_"
            const char * varType = ivar_getTypeEncoding(var);
            NSString * type = [NSString stringWithUTF8String:varType];//参数类型
            type = [type stringByReplacingOccurrencesOfString:@"@" withString:@""];//过滤@
            type = [type stringByReplacingOccurrencesOfString:@"\"" withString:@""];//过滤"字符
            type = [type stringByReplacingOccurrencesOfString:@"\\" withString:@""];//过滤\字符
            if ([name isEqualToString:@"_id"]) {
                name = [name substringWithRange:NSMakeRange(1,name.length-1)];
            }
            id value = [self filterNullValue:[array[i] objectForKey:name] andType:type];
            object_setIvar(obj, var, value);
        }
        [dataArray addObject:obj];
    }
    return dataArray;
}

#pragma mark - 过滤空值
+(id)filterNullValue:(id)value andType:(NSString *)type
{
    Class classname = NSClassFromString(type);
    id filterValue = value;
    if (!filterValue || [filterValue isKindOfClass:[NSNull class]])//处理nil或者NULL的情况
    {
        if (classname == [NSArray class]) {//处理数组
            return [[NSArray alloc] init];
        }
        if (classname == [NSString class]) {//处理字符串
            return @"";
        }
        if (classname == [NSDictionary class]) {//处理字典
            return [[NSDictionary alloc] init];
        }
        return [[classname alloc] init];//其他情况
    }
    
    return filterValue;
}

@end
